package local;

import javassist.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ModifyClassFile {
  public static void main(String[] args) {
    // 原始class文件路径
    String originalClassPath = "F:\\IdeaProjects\\FineCrack\\src\\main\\lib";
    // 修改后的class文件路径
    String modifiedClassPath = "SessionPoolManager.class";

    try {
      // 获取类池
      ClassPool pool = ClassPool.getDefault();
      // 添加类路径，确保类池可以找到目标类
      pool.insertClassPath(originalClassPath);

      // 获取目标类
      CtClass ctClass = pool.get("com.fr.web.core.SessionPoolManager");

      // 获取目标方法
      CtMethod method = ctClass.getDeclaredMethod("generateSessionID_checkPromptRegisted");

      // 修改方法体
      method.setBody("{    if (true) {\n" +
              "      boolean bool = System.currentTimeMillis()>10000000L;\n" +
              "      if (bool) {\n" +
              "        com.fr.stable.web.Weblet weblet;\n" +
              "        com.fr.stable.FRException fRException = new com.fr.stable.FRException(2102, com.fr.locale.InterProviderFactory.getProvider().getLocText(\"Fine-Core_Base_Need_Purchase_Info\"), \"Fine-Core_Base_Need_Purchase_Info\");\n" +
              "        com.fr.stable.plugin.ExtraClassManagerProvider extraClassManagerProvider = (com.fr.stable.plugin.ExtraClassManagerProvider)com.fr.plugin.injectable.PluginModule.getAgent(com.fr.plugin.injectable.PluginModule.ExtraCore);\n" +
              "        com.fr.stable.fun.UnregisteredPageProcessor unregisteredPageProcessor = null;\n" +
              "        if (extraClassManagerProvider != null)\n" +
              "          unregisteredPageProcessor = (com.fr.stable.fun.UnregisteredPageProcessor)extraClassManagerProvider.getSingle(\"UnregisteredPageProcessor\"); \n" +
              "        if (unregisteredPageProcessor != null) {\n" +
              "          weblet = unregisteredPageProcessor.createWeblet();\n" +
              "        } else {\n" +
              "          weblet = com.fr.web.factory.WebletFactory.createEmbeddedWeblet(\"/com/fr/web/tpl/lic.frm\");\n" +
              "        } \n" +
              "        fRException.setHandlerMethod(new com.fr.web.core.DefaultHandlerMethod(weblet));\n" +
              "        throw fRException;\n" +
              "      } \n" +
              "    } }");

      // 输出修改后的字节码到文件
      byte[] modifiedClassBytes = ctClass.toBytecode();
      File modifiedClassFile = new File(modifiedClassPath);
      FileOutputStream fos = new FileOutputStream(modifiedClassFile);
      fos.write(modifiedClassBytes);
      fos.close();

      System.out.println("Class file modified successfully.");

    } catch (IOException | CannotCompileException | NotFoundException e) {
      e.printStackTrace();
    }
  }





}